<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="校区">
        <el-select v-model="queryParams.campusId"
                   @change="campusHandleChange"
                   placeholder="校区">
          <el-option
            v-for="item in campusList"
            :key="item.id"
            :label="item.campusName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="学科">
        <el-select v-model="queryParams.subjectId"
                   @change="subjectHandleChange"
                   placeholder="学科">
          <el-option
            v-for="item in subjectsList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排班时间" prop="workDate">
        <el-date-picker clearable
                        v-model="queryParams.workDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择排班时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teacher:schedule:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teacher:schedule:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teacher:schedule:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teacher:schedule:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-calendar>
      <!-- 这里使用的是 2.5 slot 语法，对于新项目请使用 2.6 slot 语法-->
      <template
        slot="dateCell"
        slot-scope="{date, data}">
        <div>
          <el-row :gutter="10" class="el-calendar-table" style="display: flex;align-items: center;">
            <el-col :span="1.5">
              <p :class="data.isSelected ? 'is-selected' : ''">
                {{ data.day.split('-').slice(1).join('-') }}
              </p>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd(data)"
              >排班
              </el-button>
            </el-col>
          </el-row>
        </div>
        <div>
          <template v-for="(tag, index) in scheduleList">
            <el-tag size="mini" v-if="data.day === tag.workDate">{{ tag.teacherName }}</el-tag>
          </template>
        </div>
      </template>
    </el-calendar>

    <!-- 添加或修改考勤排班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="考勤组">
          <el-select v-model="form.groupId"
                     @change="groupsHandleChange"
                     placeholder="考勤组">
            <el-option
              v-for="item in groupsList"
              :key="item.groupId"
              :label="item.groupName"
              :value="item.groupId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="班次选择">
          <el-select v-model="form.classId"
                     @change="classesHandleChange"
                     placeholder="班次">
            <el-option
              v-for="item in classesList"
              :key="item.classId"
              :label="item.className"
              :value="item.classId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="commitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSchedule,
  getSchedule,
  delSchedule,
  addSchedule,
  updateSchedule,
  listScheduleClass
} from "@/api/teacher/schedule";
import {listCampus, loadSuperiorsObjs} from "@/api/teacher/teacher";
import Template from "@/views/performance/template/index.vue";

export default {
  name: "Schedule",
  components: {Template},
  data() {
    return {
      classesList: [],
      groupsList: [],
      campusList: [],
      subjectsList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老师排班表格数据
      scheduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        groupId: null,
        groupName: null,
        classId: null,
        className: null,
        workDate: null,
        campusId: null,
        subjectId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //查询所有的考勤组
    loadGroups() {
      listScheduleClass().then(res => {
        let {data, code} = res;
        if (code == 200) {
          this.groupsList = data;
          console.log(this.groupsList)
        }
      })
    },
    //处理选中的考勤班次
    classesHandleChange(res) {
      for (let i = 0; i < this.classesList.length; i++) {
        if (this.classesList[i].classId == res) {
          this.form.className = this.classesList[i].className;
          return;
        }
      }
    },
    //处理选中的考勤组
    groupsHandleChange(res) {
      for (let i = 0; i < this.groupsList.length; i++) {
        if (this.groupsList[i].groupId == res) {
          this.classesList = this.groupsList[i].classList;
          this.form.groupId = res;
          this.form.groupName = this.groupsList[i].groupName;
          return;
        }
      }
    },
    subjectHandleChange(res) {
      this.form.subjectId = res;
    },

    //选中校区获取学科
    campusHandleChange(res) {
      for (let i = 0; i < this.campusList.length; i++) {
        if (this.campusList[i].id == res) {
          this.subjectsList = this.campusList[i].subjects;
          this.form.campusId = res;
          return;
        }
      }
    },
    //查询校区
    getListCampus() {
      listCampus().then(res => {
        let {code, data} = res;
        if (code == 200) {
          this.campusList = data;
        }
      })
    },

    /** 查询老师排班列表 */
    getList() {
      this.loading = true;
      listSchedule(this.queryParams).then(response => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        groupId: null,
        groupName: null,
        classId: null,
        className: null,
        workDate: null,
        campusId: this.form.campusId,
        subjectId: this.form.subjectId
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(data) {
      this.reset();
      this.form.workDate = data.day;
      this.open = true;
      this.title = "添加老师排班";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSchedule(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改老师排班";
      });
    },
    commitForm() {
      addSchedule(this.form).then(response => {
        this.$modal.msgSuccess("排班成功");
        this.open = false;
        this.getList();
      });
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSchedule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchedule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除老师排班编号为"' + ids + '"的数据项？').then(function () {
        return delSchedule(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/schedule/export', {
        ...this.queryParams
      }, `schedule_${new Date().getTime()}.xlsx`)
    }
  },
  mounted() {
    this.getListCampus();
    this.loadGroups();
  }
};
</script>
<style>
.el-calendar-table .el-calendar-day {
  width: auto;
  height: auto;
}
</style>
